//
//  MBStorySceneController.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBStorySceneController.h"
#import "MBSongDetailController.h"
#import "MBSpaceAlbumViewController.h"

#import "MBSpaceMyAlbumCell.h"
#import "ScreenTypeView.h"

@interface MBStorySceneController ()<UITableViewDataSource,UITableViewDelegate,ScreenTypeViewDelegate> {
    UITableView             *_tableView;
    ScreenTypeView          *_screenTypeView;
    NSInteger               indexCount;  //歌曲列表页数
    NSMutableArray          *_mutAlbumListArr;
    
    NSString                *_fenlei;
    NSString                *_fenling;
    NSString                *_changjing;
}

@property (nonatomic,strong)NSString *cateID;
@property (nonatomic,strong)NSString *idStr;

@end

@implementation MBStorySceneController

- (void)viewDidLoad {
    [super viewDidLoad];
    indexCount = 1;
    _mutAlbumListArr = [[NSMutableArray alloc]init];
    _fenlei = @"";
    _fenling = @"";
    _changjing = @"";
    // Do any additional setup after loading the view.
    [self setNavBar];
    [self setupUI];
    
    [self onLoadNew];
}

#pragma mark - Actions
#pragma mrak - UI
- (void)setNavBar
{
    if (_type == MBStorySceneCellType_FenLei) {
        switch ([self.valueStr intValue]) {
            case 1:
            {
                self.titleString = @"儿歌";
                self.cateID = @"1";
                self.idStr = @"1";
            }
                break;
            case 2:
            {
                self.titleString = @"故事";
                self.cateID = @"1";
                self.idStr = @"2";
            }
                break;
            case 3:
            {
                self.titleString = @"英文";
                self.cateID = @"1";
                self.idStr = @"3";
            }
                break;
            case 4:
            {
                self.titleString = @"国学";
                self.cateID = @"1";
                self.idStr = @"4";
            }
                break;
                
            default:
                break;
        }
    }
    else if (_type == MBStorySceneCellType_FenLing) {
        switch ([self.valueStr intValue]) {
            case 1:
            {
                self.titleString = @"0-2岁";
                self.cateID = @"2";
                self.idStr = @"5";
            }
                break;
            case 2:
            {
                self.titleString = @"3-4岁";
                self.cateID = @"2";
                self.idStr = @"6";
            }
                break;
            case 3:
            {
                self.titleString = @"5-6岁";
                self.cateID = @"2";
                self.idStr = @"7";
            }
                break;
            case 4:
            {
                self.titleString = @"6岁以上";
                self.cateID = @"2";
                self.idStr = @"8";
            }
                break;
                
            default:
                break;
        }
    }
    else if (_type == MBStorySceneCellType_ChangJing) {
        switch ([self.valueStr intValue]) {
            case 1:
            {
                self.titleString = @"睡前安抚";
                self.cateID = @"3";
                self.idStr = @"9";
            }
                break;
            case 2:
            {
                self.titleString = @"父母学堂";
                self.cateID = @"3";
                self.idStr = @"10";
            }
                break;
            case 3:
            {
                self.titleString = @"习性养成";
                self.cateID = @"3";
                self.idStr = @"11";
            }
                break;
            case 4:
            {
                self.titleString = @"智力开发";
                self.cateID = @"3";
                self.idStr = @"12";
            }
                break;
                
            default:
                break;
        }
    }
   
    
    [self setTitleView:self.titleString];
    [self setBackItem];
    
    if (self.isAlbumForSearch) {
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(0, 0, 60, 30);
        [bt setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
        [bt setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateHighlighted];
        [bt setImageEdgeInsets:UIEdgeInsetsMake(-40, 0, -40, -40)];
        [bt addTarget:self action:@selector(onSelectItemForSearch:) forControlEvents:UIControlEventTouchUpInside];
        [self setRightBarItems:bt];
    } else {
        [self setRightPlayerItem];
    }
}

- (void)setupUI {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
    [_tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    
    //筛选弹出框
    _screenTypeView = [[ScreenTypeView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 0)];
    _screenTypeView.delegate = self;
    _screenTypeView.orientation = self.orientation;
    _screenTypeView.hidden = (self.isAlbumForSearch?NO:YES);
    [self.view addSubview:_screenTypeView];
    [self.view insertSubview:_screenTypeView aboveSubview:_tableView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    if (!self.isAlbumForSearch) {
        _screenTypeView.orientation = self.orientation;
    }
    [_tableView reloadData];
}

- (void)onLoadNew
{
    indexCount = 1;
    [self loadData];
}

- (void)onLoadMore
{
    ++indexCount;
    [self loadData];
}

- (void)loadData
{
    if (self.type == MBStorySceneCellType_AlbumList) {
        //热门专辑
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSString *url = [NSString stringWithFormat:STORY_HOT_ALBUM_LIST_AUDIOLIST_URL_PATH,[NSString stringWithFormat:@"%ld",indexCount],_fenlei,_fenling,_changjing];
        [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            if ([json isKindOfClass:[NSDictionary class]]) {
                if ([json[@"ret_code"] integerValue]==0) {
                    NSArray *arr = json[@"data"];
                    if (arr.count > 0) {
                        if (indexCount == 1) {
                            [_mutAlbumListArr removeAllObjects];
                        }
                        [_mutAlbumListArr addObjectsFromArray:arr];
                    }
                    [_tableView reloadData];
                }
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
        }];
    }
    else if (self.type == MBStorySceneCellType_HotAnchor) {
        //人气主播
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSString *url = [NSString stringWithFormat:STORY_HOT_ANCHOR_LIST_AUDIOLIST_URL_PATH,[NSString stringWithFormat:@"%ld",indexCount]];
        [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            
            if ([json isKindOfClass:[NSDictionary class]]) {
                if ([json[@"ret_code"] integerValue]==0) {
                    NSArray *arr = json[@"data"];
                    if (arr.count > 0) {
                        if (indexCount == 1) {
                            [_mutAlbumListArr removeAllObjects];
                        }
                        [_mutAlbumListArr addObjectsFromArray:arr];
                    }
                    [_tableView reloadData];
                }
            }

        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];

        }];
    }
    else {
        if(self.cateID.length == 0) return;
        if(self.idStr.length == 0) return;
        //分类、分龄、场景
        NSString *url = [NSString stringWithFormat:STORY_THREE_ALBUM_CONTAIN_AUDIOLIST_URL_PATH,self.cateID,self.idStr,[NSString stringWithFormat:@"%ld",indexCount]];
        [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(NSDictionary *json) {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            
            if ([json isKindOfClass:[NSDictionary class]]) {
                if ([json[@"ret_code"] integerValue]==0) {
                    NSArray *arr = json[@"data"];
                    if (arr.count > 0) {
                        if (indexCount == 1) {
                            [_mutAlbumListArr removeAllObjects];
                        }
                        if (arr.count < 10) {
                            //当前页不足10个，下页数据为空，页数-1
                            indexCount--;
                        }
                        
                        [_mutAlbumListArr addObjectsFromArray:arr];
                    }
                    [_tableView reloadData];
                } else {
                    [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
                }
            }
        } failure:^(NSError *error) {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
        }];
    }
    
}

#pragma mark - 筛选
- (void)onSelectItemForSearch:(UIButton *)bt
{		
    bt.selected = !bt.selected;
    _screenTypeView.orientation = self.orientation;
    [_screenTypeView showInView:bt.selected];
}

- (void)onShowScreenTypeWithFenLei:(int)fenlei withFenLing:(int)fenling withChangJiang:(int)changjing withView:(ScreenTypeView *)view
{
    if (fenlei == 100) {
        _fenlei = @"";
    } else {
        _fenlei = [NSString stringWithFormat:@"%d",fenlei+1];
    }
    if (fenling == 100) {
        _fenling = @"";
    } else {
        _fenling = [NSString stringWithFormat:@"%d",fenling+1];
    }
    if (changjing == 100) {
        _changjing = @"";
    } else {
        _changjing = [NSString stringWithFormat:@"%d",changjing+1];
    }
    [_mutAlbumListArr removeAllObjects];
    [self loadData];
}

#pragma mark - 播放器
- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutAlbumListArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Collection_Album_Cell";
    MBSpaceMyAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBSpaceMyAlbumCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (_mutAlbumListArr.count > indexPath.row) {
        cell.dataDic = [_mutAlbumListArr objectAtIndex:indexPath.row];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
#pragma mark - 人气主播-专辑模式
    if (_mutAlbumListArr.count > indexPath.row) {
        NSDictionary *dataDic = [_mutAlbumListArr objectAtIndex:indexPath.row];
        if (self.type == MBStorySceneCellType_AlbumList) {
            NSString *AlbumStatus = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"AlbumStatus"]];
            if ([AlbumStatus intValue] == 0) {
                //不可点击
//                return;
            }
        }
        MBSpaceAlbumViewController *song = [[MBSpaceAlbumViewController alloc]init];
        if (_type == MBStorySceneCellType_HotAnchor) {
            song.titleString = [dataDic objectForKey:@"NickName"];
        } else {
            song.titleString = [dataDic objectForKey:@"AlbumName"];
        }
        song.type = _type;
        song.isFormStroy = YES;
        song.dateDic = dataDic;
        [self.navigationController pushViewController:song animated:YES];
    }

}

@end
